﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bigger.Base;
using Bigger.Base.Cache;
using Bigger.Framework;
using System.Threading;
using System.Data;
using Bigger.Study.Business;
using Bigger.Study.DataEntity;
using Bigger.Core.Service;
using Bigger.Core.Logging;

namespace Bigger.Study.ConsoleApp
{
    public class BiggerCacheExample : IExample
    {
        public void Execute()
        {
            //Register();

            GetCache();

            //DeleteCache();
        }
        void Register()
        {
            //初始化服务
            UnityConfiguration.UnityConfig.InitialConfig();
            BaseConfiguration.RegisterWriteSqlLog((sender, e) =>
            {
                ILogService objLogService = LogConfiguration.LogServiceProvider.CreateLogService();
                objLogService.WriteSqlLog(e.DbConnection, e.SqlString, e.DbParameters);
            });
        }

        void GetCache()
        {
            BizCourse objBizCourse = new BizCourse();
            Course objCourse1 = objBizCourse.GetRecordCache("", Guid.Parse("62F15DDA-BB97-422B-B3AD-88F163C72376"));
            Course objCourse2 = objBizCourse.GetRecordCache("", Guid.Parse("62F15DDA-BB97-422B-B3AD-88F163C72376"));
            Console.WriteLine(object.ReferenceEquals(objCourse1, objCourse2));
            objBizCourse.DeleteIDCache("", Guid.Parse("62F15DDA-BB97-422B-B3AD-88F163C72376"));
            Course objCourse3 = objBizCourse.GetRecordCache("", Guid.Parse("62F15DDA-BB97-422B-B3AD-88F163C72376"));
            Console.WriteLine(object.ReferenceEquals(objCourse2, objCourse3));
            Course objCourse4 = objBizCourse.GetRecordCache("", Guid.Parse("62F15DDA-BB97-422B-B3AD-88F163C72376"));
            Console.WriteLine(object.ReferenceEquals(objCourse3, objCourse4));
            Course objCourse5 = objBizCourse.GetRecordCache("自定义key1", Guid.Parse("62F15DDA-BB97-422B-B3AD-88F163C72376"));
            Console.WriteLine(object.ReferenceEquals(objCourse4, objCourse5));
            Course objCourse6 = objBizCourse.GetRecordCache("自定义key1", Guid.Parse("62F15DDA-BB97-422B-B3AD-88F163C72376"));
            Console.WriteLine(object.ReferenceEquals(objCourse5, objCourse6));
            objBizCourse.DeleteIDCache("自定义key1", Guid.Parse("62F15DDA-BB97-422B-B3AD-88F163C72376"));
            Course objCourse7 = objBizCourse.GetRecordCache("自定义key1", Guid.Parse("62F15DDA-BB97-422B-B3AD-88F163C72376"));
            Console.WriteLine(object.ReferenceEquals(objCourse6, objCourse7));


            //List<Course> objList1 = objBizCourse.GetListCache("","", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //List<Course> objList2 = objBizCourse.GetListCache("","", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList1, objList2));
            ////其他调用方法一样，只需传入依赖键dependentKey和缓存时间expiry，后面的参数与原生的调用方式一致

            /*****************************************************************************************************/

            //BizBase objBizBase = new BizBase();

            //DataSet objDataSet1 = objBizBase.ExecuteDataSetCache("", "", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //DataSet objDataSet2 = objBizBase.ExecuteDataSetCache("", "", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet1, objDataSet2));
            //其他调用方法一样，只需传入依赖键dependentKey和缓存时间expiry，后面的参数与原来的api调用方式一致
        }

        void DeleteCache()
        {
            //BizCourse objBizCourse = new BizCourse();

            ////通过删除CacheKey清除缓存
            //List<Course> objList1 = objBizCourse.GetListCache("", "", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //List<Course> objList2 = objBizCourse.GetListCache("", "", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList1, objList2));
            //string cacheKey = objBizCourse.CreateCacheKey("GetListCache", "StartTime>@StartTime", TimeSpan.FromSeconds(30), new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //objBizCourse.DeleteCache("", cacheKey);
            //Console.WriteLine("删除CacheKey");
            //List<Course> objList3 = objBizCourse.GetListCache("", "", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList2, objList3));

            //List<Course> objList4 = objBizCourse.GetListCache("", "自定义key1", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            ////Console.WriteLine(object.ReferenceEquals(objList3, objList4));
            //List<Course> objList5 = objBizCourse.GetListCache("", "自定义key1", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList4, objList5));
            //objBizCourse.DeleteCache("", "自定义key1");
            //Console.WriteLine("删除自定义key1");
            //List<Course> objList6 = objBizCourse.GetListCache("", "自定义key1", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList5, objList6));


            ////通过删除DependentKey清除所有在同一依赖键下的缓存
            //List<Course> objList1 = objBizCourse.GetListCache("","", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //List<Course> objList2 = objBizCourse.GetListCache("","", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList1, objList2));
            //DataSet objDataSet1 = objBizCourse.GetPageDataSetCache("","", TimeSpan.FromSeconds(30), 0, 10, true);
            //DataSet objDataSet2 = objBizCourse.GetPageDataSetCache("","", TimeSpan.FromSeconds(30), 0, 10, true);
            //Console.WriteLine(object.ReferenceEquals(objDataSet1, objDataSet2));
            //objBizCourse.DeleteDependentCache("");
            //Console.WriteLine("删除DependentKey");
            //List<Course> objList3 = objBizCourse.GetListCache("","", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList2, objList3));
            //DataSet objDataSet3 = objBizCourse.GetPageDataSetCache("","", TimeSpan.FromSeconds(30), 0, 10, true);
            //Console.WriteLine(object.ReferenceEquals(objDataSet2, objDataSet3));
            //List<Course> objList4 = objBizCourse.GetListCache("","", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList3, objList4));
            //DataSet objDataSet4 = objBizCourse.GetPageDataSetCache("","", TimeSpan.FromSeconds(30), 0, 10, true);
            //Console.WriteLine(object.ReferenceEquals(objDataSet3, objDataSet4));


            ////如果不想因删除DependentKey而删除所有的缓存，请指定不同的DependentKey
            //List<Course> objList1 = objBizCourse.GetListCache("List_Cache", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //List<Course> objList2 = objBizCourse.GetListCache("List_Cache", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList1, objList2));
            //DataSet objDataSet1 = objBizCourse.GetPageDataSetCache("","", TimeSpan.FromSeconds(30), 0, 10, true);
            //DataSet objDataSet2 = objBizCourse.GetPageDataSetCache("","", TimeSpan.FromSeconds(30), 0, 10, true);
            //Console.WriteLine(object.ReferenceEquals(objDataSet1, objDataSet2));
            //objBizCourse.DeleteDependentCache("List_Cache");
            //Console.WriteLine("删除DependentKey");
            //List<Course> objList3 = objBizCourse.GetListCache("List_Cache", TimeSpan.FromSeconds(30), "StartTime>@StartTime", new { StartTime = DateTime.Parse("2015-12-07 11:41:52") });
            //Console.WriteLine(object.ReferenceEquals(objList2, objList3));
            //DataSet objDataSet3 = objBizCourse.GetPageDataSetCache("","", TimeSpan.FromSeconds(30), 0, 10, true);
            //Console.WriteLine(object.ReferenceEquals(objDataSet2, objDataSet3));

            /*****************************************************************************************************/

            //BizBase objBizBase = new BizBase();

            //////通过删除CacheKey清除缓存
            ////DataSet objDataSet1 = objBizBase.ExecuteDataSetCache("", "", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            ////DataSet objDataSet2 = objBizBase.ExecuteDataSetCache("", "", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            ////Console.WriteLine(object.ReferenceEquals(objDataSet1, objDataSet2));
            ////ApiContext objApiContext = objBizBase.CreateApiContext("Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            ////string cacheKey = objBizBase.CreateCacheKey(objApiContext, "ExecuteDataSetCache", TimeSpan.FromSeconds(30));
            ////objBizBase.DeleteCache("", "Bigger_Course", "ApiGroup_Course", cacheKey);
            ////Console.WriteLine("删除CacheKey");
            ////DataSet objDataSet3 = objBizBase.ExecuteDataSetCache("", "", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            ////Console.WriteLine(object.ReferenceEquals(objDataSet2, objDataSet3));
            ////DataSet objDataSet4 = objBizBase.ExecuteDataSetCache("", "", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            ////Console.WriteLine(object.ReferenceEquals(objDataSet3, objDataSet4));
            //DataSet objDataSet5 = objBizBase.ExecuteDataSetCache("", "自定义key1", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            ////Console.WriteLine(object.ReferenceEquals(objDataSet4, objDataSet5));
            //DataSet objDataSet6 = objBizBase.ExecuteDataSetCache("", "自定义key1", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet5, objDataSet6));
            //objBizBase.DeleteCache("", "Bigger_Course", "ApiGroup_Course", "自定义key1");
            //DataSet objDataSet7 = objBizBase.ExecuteDataSetCache("", "自定义key1", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet6, objDataSet7));
            //DataSet objDataSet8 = objBizBase.ExecuteDataSetCache("", "自定义key1", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet7, objDataSet8));

            ////通过删除DependentKey清除所有在同一依赖键下的缓存
            //DataSet objDataSet1 = objBizBase.ExecuteDataSetCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //DataSet objDataSet2 = objBizBase.ExecuteDataSetCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet1, objDataSet2));
            //ApiContext objApiContext = objBizBase.CreateApiContext("Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //List<Dictionary<string, object>> objList1 = objBizBase.GetListDictionaryCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 1, PageSize = 2 });
            //List<Dictionary<string, object>> objList2 = objBizBase.GetListDictionaryCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 1, PageSize = 2 });
            //Console.WriteLine(object.ReferenceEquals(objList1, objList2));
            //objBizBase.DeleteDependentCache("", "Bigger_Course", "ApiGroup_Course");
            //Console.WriteLine("删除DependentKey");
            //DataSet objDataSet3 = objBizBase.ExecuteDataSetCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet2, objDataSet3));
            //List<Dictionary<string, object>> objList3 = objBizBase.GetListDictionaryCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 1, PageSize = 2 });
            //Console.WriteLine(object.ReferenceEquals(objList2, objList3));
            //DataSet objDataSet4 = objBizBase.ExecuteDataSetCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet3, objDataSet4));
            //List<Dictionary<string, object>> objList4 = objBizBase.GetListDictionaryCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 1, PageSize = 2 });
            //Console.WriteLine(object.ReferenceEquals(objList3, objList4));


            ////如果不想因删除DependentKey而删除所有的缓存，请指定不同的DependentKey
            //DataSet objDataSet1 = objBizBase.ExecuteDataSetCache("DataSet_Cache", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //DataSet objDataSet2 = objBizBase.ExecuteDataSetCache("DataSet_Cache", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet1, objDataSet2));
            //ApiContext objApiContext = objBizBase.CreateApiContext("Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //List<Dictionary<string, object>> objList1 = objBizBase.GetListDictionaryCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 1, PageSize = 2 });
            //List<Dictionary<string, object>> objList2 = objBizBase.GetListDictionaryCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 1, PageSize = 2 });
            //Console.WriteLine(object.ReferenceEquals(objList1, objList2));
            //objBizBase.DeleteDependentCache("DataSet_Cache", "Bigger_Course", "ApiGroup_Course");
            //Console.WriteLine("删除DependentKey");
            //DataSet objDataSet3 = objBizBase.ExecuteDataSetCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet2, objDataSet3));
            //List<Dictionary<string, object>> objList3 = objBizBase.GetListDictionaryCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 1, PageSize = 2 });
            //Console.WriteLine(object.ReferenceEquals(objList2, objList3));
            //DataSet objDataSet4 = objBizBase.ExecuteDataSetCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CourseDataSet", new { CourseId = "62F15DDA-BB97-422B-B3AD-88F163C72376" });
            //Console.WriteLine(object.ReferenceEquals(objDataSet3, objDataSet4));
            //List<Dictionary<string, object>> objList4 = objBizBase.GetListDictionaryCache("","", TimeSpan.FromSeconds(30), "Bigger_Course", "ApiGroup_Course", "Bigger_CoursePage", new { PageIndex = 1, PageSize = 2 });
            //Console.WriteLine(object.ReferenceEquals(objList3, objList4));
        }
    }
}
